﻿using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Utilities;
using Process.Entities;
using System.Collections.Generic;

namespace Process.DAL
{
    public class ProviderDAL
    {
        private static ProviderENT CreateProviderFromReader(IDataReader reader)
        {
            ProviderENT item = new ProviderENT();

            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderID.ToString()))) item.ProviderID = Utility.StringParse(reader[ProviderColumns.ProviderID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderUsername.ToString()))) item.ProviderUsername = Utility.StringParse(reader[ProviderColumns.ProviderUsername.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderPassword.ToString()))) item.ProviderPassword = Utility.StringParse(reader[ProviderColumns.ProviderPassword.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.BusinessUsername.ToString()))) item.BusinessUsername = Utility.StringParse(reader[ProviderColumns.BusinessUsername.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.BusinessPassword.ToString()))) item.BusinessPassword = Utility.StringParse(reader[ProviderColumns.BusinessPassword.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderName.ToString()))) item.ProviderName = Utility.StringParse(reader[ProviderColumns.ProviderName.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderContactPerson.ToString()))) item.ProviderContactPerson = Utility.StringParse(reader[ProviderColumns.ProviderContactPerson.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderAddress.ToString()))) item.ProviderAddress = Utility.StringParse(reader[ProviderColumns.ProviderAddress.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderPhone.ToString()))) item.ProviderPhone = Utility.StringParse(reader[ProviderColumns.ProviderPhone.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.ProviderPaymentCard.ToString()))) item.ProviderPaymentCard = Utility.StringParse(reader[ProviderColumns.ProviderPaymentCard.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.WS1.ToString()))) item.WS1 = Utility.StringParse(reader[ProviderColumns.WS1.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.WS2.ToString()))) item.WS2 = Utility.StringParse(reader[ProviderColumns.WS2.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.WS3.ToString()))) item.WS3 = Utility.StringParse(reader[ProviderColumns.WS3.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.WS4.ToString()))) item.WS4 = Utility.StringParse(reader[ProviderColumns.WS4.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.WS5.ToString()))) item.WS5 = Utility.StringParse(reader[ProviderColumns.WS5.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(ProviderColumns.IsDeleted.ToString()))) item.IsDeleted = Utility.BoolParse(reader[ProviderColumns.IsDeleted.ToString()]);

            return item;
        }

        public static ProviderENT GetProviderByUsername(string username)
        {
            try
            {
                ProviderENT provider = new ProviderENT();

                DataAccessHelper.AddParameter("Username", SqlDbType.VarChar, username);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procProviderGetByUsername"))
                {
                    while (reader.Read())
                    {
                        provider = CreateProviderFromReader(reader);
                    }
                }

                return provider;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà cung cấp.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public static ProviderENT GetProviderByID(string providerID)
        {
            try
            {
                ProviderENT provider = new ProviderENT();

                DataAccessHelper.AddParameter("ProviderID", SqlDbType.VarChar, providerID);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procProviderGetByID"))
                {
                    while (reader.Read())
                    {
                        provider = CreateProviderFromReader(reader);
                    }
                }

                return provider;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà cung cấp.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public static List<ProviderENT> GetProviderList()
        {
            try
            {
                List<ProviderENT> providerList = new List<ProviderENT>();

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procProviderGetList"))
                {
                    while (reader.Read())
                    {
                        providerList.Add(CreateProviderFromReader(reader));
                    }
                }

                return providerList;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin nhà cung cấp.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}